package Dao.impl;

import Dao.BaseDao;
import Dao.UserDao;
import pojo.User;

import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
    // 创建一个userBaseDao工具类，方便操作数据库
    @Override
    public boolean insert(User user) {
        // 下面给我写一个mySql语句，用来插入用户，主要包括username, password, email, fullName个字段
        String sql = "insert into users(username, password, email, fullName, profilePicture, status) values(?, ?, ?, ?, ?, ?)";
        // 添加数据是用update
        int result = update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getFullName(), user.getProfilePicture(), user.getStatus());
        return result > 0;
    }

    @Override
    public User login(User user) {
        User result = null;
        // 下面给我写一个 mySql 语句，用来查询用户，主要包括 username, password, email, fullName 个字段
        String sql = "select * from users where username =? and password =?";
        // 查询数据是用
        List<User> userList = getBeanList(sql, user.getUsername(), user.getPassword());
        if (userList.size() > 0) {
            result = userList.get(0);
        }
        return result;
    }

    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
        User result = null;
        String sql = "SELECT * FROM users WHERE username =? AND password =?";
        List<User> userList = getBeanList(sql, username, password);
        if (userList.size() > 0) {
            result = userList.get(0);
        }
        return result;
    }

    @Override
    public boolean updateUser(User user) {
        String sql = "UPDATE users SET username =?, password =?, email =?, fullName =? WHERE userId =?";
        int result = update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getFullName(), user.getUserId());
        return result > 0;
    }

    @Override
    public User getUserById(int userId) {
        String sql = "SELECT * FROM users WHERE userId =?";
        return getBean(sql, userId);
    }

    @Override
    public boolean deleteUploadedSong(int songId) {
        String sql = "DELETE FROM songs WHERE songId =?";
        int result = update(sql, songId);
        return result > 0;
    }

    //修改用户密码
    @Override
    public boolean changeUserPassword(int userId, String newPassword) {
        String sql = "UPDATE users SET password =? WHERE userId =?";
        int result = update(sql, newPassword, userId);
        return result > 0;
    }

    @Override
    public boolean updatePassword(User user) {
        String sql = "UPDATE users SET password =? WHERE userId =?";
        int result = update(sql, user.getPassword(), user.getUserId());
        return result > 0;
    }

    @Override
    public boolean removeById(int userId) {
        String sql = "delete from users where userId = ?";
        int result = update(sql, userId);
        return result > 0;
    }

    @Override
    public boolean updateUserNotPassword(User user) {
        String sql = "UPDATE users SET username =?, email =?, fullName =? WHERE userId =?";
        int result = update(sql, user.getUsername(), user.getEmail(), user.getFullName(), user.getUserId());
        return result > 0;
    }

    @Override
    public List<User> selectUserList() {
        String sql = "SELECT * FROM users";
        List<User> userList = getBeanList(sql);
        return userList;
    }

    @Override
    public List<User> selectUserListByStatus(String status) {
        // service层已经判断username是否为空，下面根据status查询userList
        String sql = "SELECT * FROM users WHERE status =?";
        List<User> userList = getBeanList(sql, status);
        return userList;
    }

    @Override
    public List<User> selectUserListByName(String nickName) {
        String sql = "SELECT * FROM users WHERE fullName =?";
        List<User> userList = getBeanList(sql, nickName);
        return userList;
    }

    @Override
    public List<User> selectUserListByNameAndStatus(String nickName, String status) {
//        昵称模糊查询
        String sql = "SELECT * FROM users WHERE fullName like concat('%', ?, '%') AND status =?";
        List<User> userList = getBeanList(sql, nickName, status);
        return userList;
    }

    @Override
    public List<User> selectUserList(Integer startIndex, Integer targetTotal) {
        String sql = "SELECT * FROM users LIMIT ?, ?";
        List<User> userList = getBeanList(sql, startIndex, targetTotal);
        return userList;
    }

}
